/*
 OFWBooter is a homebrew that loads the official firmware of your current custom firmware
 Copyright (C) 2010  dxem
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 */

#include <pspkernel.h>
#include <psploadexec_kernel.h>

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "rebootex.h"

PSP_MODULE_INFO("ofwboot_kernel", 0x1000, 1, 1);

#define JAL_OPCODE	0x0C000000
#define MAKE_CALL(a, f) _sw(JAL_OPCODE | (((u32)(f) >> 2) & 0x03ffffff), a);

/* A struct holding the offsets of the loadexec-instructions to be patched */
typedef struct _loadexecPatches
{
	int version;				/* The firmware using this offsets */
	u32 decompressRebootCall;	/* The offset of the instruction which calls decompressReboot */
	u32 sceRebootAddrload;		/* The offset of the instruction which loads the SceReboot address */
} loadexecPatches;

#define FIRMWARE_COUNT 4
loadexecPatches patches[FIRMWARE_COUNT] =
{
	{0x05000010, 0x27DC, 0x2820}, /* 5.00 */
	{0x05000210, 0x27DC, 0x2820}, /* 5.02 */
	{0x05000310, 0x27DC, 0x2820}, /* 5.03 */
	{0x06020010, 0x2CD8, 0x2D24}  /* 6.20 */
};

int (* DecompressReboot) (u32 addr, int unk1, int unk2, int unk3, int unk4) = NULL; /* Dummy function */

int DecompressRebootPatch(u32 addr, int unk1, int unk2, int unk3, int unk4)
{
	memcpy((u8 *)0x88fb0000, rebootex, size_rebootex); /* Copy rebootex to 0x88FC0000 */
	return DecompressReboot(addr, unk1, unk2, unk3, unk4); /* Call original DecompressReboot and return */
};

int dxemRebootCheckAndPatch(int textaddr)
{
	int kernel = sceKernelDevkitVersion(); /* Get Devkit/Firmware version again.. */
	
	int i = 0;
	
	/* Search the right loadexecPatches struct */
	while(i < FIRMWARE_COUNT)
	{
		if(patches[i].version == kernel)
		{
			/* Hook the DecompressReboot call */
			MAKE_CALL(textaddr + patches[i].decompressRebootCall, DecompressRebootPatch);
			
			/* Store "lui $at, 0x88fb", for making loadexec to load the rebootex instead of SceReboot */
			_sw(0x3C0188fb, textaddr + patches[i].sceRebootAddrload);
			
			break;
		};
		
		i++;
	};
	
	return 0;
};

int dxemRebootOFW(void)
{
	SceModule2 *mod = sceKernelFindModuleByName("sceLoadExec"); /* Find sceLoadExec */
	if(mod)
	{
		int textaddr = mod->text_addr; /* Get text_addr */
		
		DecompressReboot = (void *)textaddr; /* Not anymore a dummy function ;D */
		sceKernelDcacheWritebackAll();
		
		dxemRebootCheckAndPatch(textaddr); /* Check Firmware and Patch loadexec */
		
		sceKernelDcacheWritebackAll();
	};
	
	return sceKernelExitVSHVSH(NULL); /* Reboot to VSH */
};

int module_start(SceSize args, void *argp)
{
	return 0;
};

int module_stop(SceSize args, void *argp)
{
	return 0;
};
